<?php
namespace Modules\User\Http\Controllers\Admin\V1;

use Illuminate\Http\Request;
use Illuminate\Routing\Controller;

use \Modules\User\Entities\User;

use Validator;
use ThrowException;
use DB;
use Auth;

class UserRedpackController extends Controller
{
    // 更改用户积分
    public function update(Request $req, $uuid)
    {
        $rule = [
            'redpack' => ['integer', 'required']
        ];
        Validator::make($req->all(), $rule)->fails() && ThrowException::BadRequest('请正确填写数量'); 

        $admin = Auth::requireLoginAdmin();

        $user = User::getEntity([
            'uuid' => $uuid
        ]);

        DB::beginTransaction();


        if ($req->type === 'in') {
            $user->asset('redpack')->in($req->redpack, [
                'description' => $req->input('description'),
                'admin' => $admin,
                'sub_type' => 2
            ]);
        }
        elseif ($req->type === 'out') {
            $user->asset('redpack')->out($req->redpack, [
                'description' => $req->input('description'),
                'admin' => $admin,
                'sub_type' => 2
            ]) || ThrowException::Conflict('操作失败，用户积分不足');
        }

        DB::commit();

        return [];
    }
}
